/* Copyright (C) 2005-2013, Unigine Corp. All rights reserved.
 *
 * File:    fragment_leaf_light.h
 * Desc:    Mesh leaf light shader
 * Version: 1.03
 * Author:  Alexander Zapryagaev <frustum@unigine.com>
 *
 * This file is part of the Unigine engine (http://unigine.com/).
 *
 * Your use and or redistribution of this software in source and / or
 * binary form, with or without modification, is subject to: (i) your
 * ongoing acceptance of and compliance with the terms and conditions of
 * the Unigine License Agreement; and (ii) your inclusion of this notice
 * in any version of this software that you use or redistribute.
 * A copy of the Unigine License Agreement is available by contacting
 * Unigine Corp. at http://unigine.com/
 */

/******************************************************************************\
*
* OpenGL/OpenGLES
*
\******************************************************************************/

#ifdef OPENGL || OPENGLES
	
	#ifndef FRAGMENT_LEAF_LIGHT
		#define FRAGMENT_LEAF_LIGHT
		
		uniform sampler2D s_texture_0;
		uniform sampler2D s_texture_1;
		uniform sampler2D s_texture_2;
		uniform sampler2D s_texture_3;
		
		uniform fixed translucent_scale;
		uniform fixed thickness_scale;
		
	#else
		
		#include <core/shaders/simple/mesh/fragment_leaf_sample.h>
		
		#ifdef WORLD
			fixed3 light_direction = s_texcoord_1.xyz;
		#else
			half4 light_direction = s_texcoord_1;
			half light_direction_length2 = dot(light_direction.xyz,light_direction.xyz);
			half light_direction_ilength = rsqrt(light_direction_length2);
			#ifdef QUALITY_MEDIUM && PHONG
				light_direction.xyz *= light_direction_ilength;
			#else
				light_direction.z *= light_direction_ilength;
			#endif
		#endif
		
		fixed thickness = texture2D(s_texture_1,s_texcoord_0.xy).x;
		fixed offset = saturate(translucent_scale - thickness * thickness_scale);
		
		#ifdef QUALITY_MEDIUM && PHONG
			fixed3 normal = texture2DNormal(s_texture_2,s_texcoord_0.xy);
			fixed4 specular = texture2D(s_texture_3,s_texcoord_0.xy);
			
			half3 camera_direction = s_texcoord_2.xyz;
			fixed3 color = diffuse.xyz * (saturate(dot(light_direction.xyz,normal) * (1.0f - offset) + offset) * s_material_shading.x);
			color += getSpecularNormalize(s_material_shading,specular.xyz,normal,light_direction.xyz,camera_direction);
		#else
			fixed3 color = diffuse.xyz * (saturate(light_direction.z * (1.0f - offset) + offset) * s_material_shading.x);
		#endif
		
		#ifndef WORLD
			fixed attenuation = getAttenuation(light_direction.w - light_direction_length2);
			color *= attenuation;
		#endif
		
	#endif
	
/******************************************************************************\
*
* Direct3D11/Direct3D119
*
\******************************************************************************/

#elif DIRECT3D11 || DIRECT3D119
	
	#ifndef FRAGMENT_LEAF_LIGHT
		#define FRAGMENT_LEAF_LIGHT
		
		Texture2D s_texture_0 : register(t0);
		Texture2D s_texture_1 : register(t1);
		Texture2D s_texture_2 : register(t2);
		Texture2D s_texture_3 : register(t3);
		
		cbuffer shader_parameters {
			float translucent_scale;
			float thickness_scale;
		};
		
	#else
		
		#include <core/shaders/simple/mesh/fragment_leaf_sample.h>
		
		#ifdef WORLD
			half3 light_direction = IN.texcoord_1.xyz;
		#else
			half4 light_direction = IN.texcoord_1;
			half light_direction_length2 = dot(light_direction.xyz,light_direction.xyz);
			half light_direction_ilength = rsqrt(light_direction_length2);
			#ifdef QUALITY_MEDIUM && PHONG
				light_direction.xyz *= light_direction_ilength;
			#else
				light_direction.z *= light_direction_ilength;
			#endif
		#endif
		
		half thickness = s_texture_1.Sample(s_sampler_1,IN.texcoord_0.xy).x;
		half offset = saturate(translucent_scale - thickness * thickness_scale);
		
		#ifdef QUALITY_MEDIUM && PHONG
			half3 normal = texture2DNormal(s_texture_2,s_sampler_2,IN.texcoord_0.xy);
			half4 specular = s_texture_3.Sample(s_sampler_3,IN.texcoord_0.xy);
			
			half3 camera_direction = IN.texcoord_2.xyz;
			half3 color = diffuse.xyz * (saturate(dot(light_direction.xyz,normal) * (1.0f - offset) + offset) * s_material_shading.x);
			color += getSpecularNormalize(s_material_shading,specular.xyz,normal,light_direction.xyz,camera_direction);
		#else
			half3 color = diffuse.xyz * (saturate(light_direction.z * (1.0f - offset) + offset) * s_material_shading.x);
		#endif
		
		#ifndef WORLD
			half attenuation = getAttenuation(light_direction.w - light_direction_length2);
			color *= attenuation;
		#endif
		
	#endif
	
#endif
